package cn.edu.sdjzu.xg.kcsj.controller.basic;

import cn.edu.sdjzu.xg.kcsj.dao.SADao;
import cn.edu.sdjzu.xg.kcsj.exception.BysjException;
import cn.edu.sdjzu.xg.kcsj.security.User;
import cn.edu.sdjzu.xg.kcsj.util.JdbcHelper;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/saFindStudentsname")
public class SaFindStudentController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            Connection connection = JdbcHelper.getConn();
            Collection<User> students_name_str = SADao.getInstance().getAllStudent(connection);
            response.getWriter().println(students_name_str);
        } catch (
                BysjException e) {
            respMessage_jsonObj.put("message", e.getMessage());
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        } catch (
                SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            response.getWriter().println(respMessage_jsonObj);
            // 在命令行打印异常信息在程序中出错的位置及原因
            e.printStackTrace();
        }
    }
}
